#include "adc1.h"


/**
  * @brief  Configure ADC2 Continuous Conversion with End Of Conversion interrupt 
  *         enabled .
  	PD3:ADC_IN4
  * @param  None
  * @retval None
  */
 void adc1_config(void)
{
  /*  PD3:Init GPIO for ADC2 */
  GPIO_Init(GPIOD, GPIO_PIN_3, GPIO_MODE_IN_FL_NO_IT);
  
  /* De-Init ADC peripheral*/
  ADC1_DeInit();

  /* Init ADC2 peripheral */
  ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_4, ADC1_PRESSEL_FCPU_D2, \
            ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL4,\
            DISABLE);
  ADC1_StartConversion();

#if 0
  /* Enable EOC interrupt */
  ADC1_ITConfig(ADC1_IT_EOC, ENABLE);

  /* Enable general interrupts */  
  enableInterrupts();
  
  /*Start Conversion */
  ADC1_StartConversion();
#endif  
}

uint16_t get_adc_value(){
	uint16_t error_cnt = 0;
	uint16_t Conversion_Value = 0;

	ADC1_ClearFlag(ADC1_FLAG_EOC);
	ADC1_StartConversion();
	while(!ADC1_GetFlagStatus(ADC1_FLAG_EOC)){
		if (error_cnt++ > 10000)
			break;
	}
	Conversion_Value  = ADC1_GetConversionValue(); 

	return Conversion_Value;
}

